<template>
  <div>
    <section class="banner">
      <div class="title">您的问题咨询处</div>
      <div>
        <el-button type="primary" class="add_btn" @click="addShow = true">
          提交新问题
        </el-button>
      </div>
      <div class="ban"></div>
    </section>
    <p class="con-title">
      <span>
        <img src="@/assets/img/control/content/Group769.png" />
      </span>
      <span>工单列表</span>
    </p>
    <el-table border :data="dataList" header-cell-class-name="title-style">
      <el-table-column prop="orderNum" label="单号" align="center" />
      <el-table-column prop="carEnums" label="类型" align="center">
        <template #default="scope">
          {{ scope.row.problemType == 1 ? "售后问题" : "产品问题" }}
        </template>
      </el-table-column>
      <el-table-column prop="problemDescription" label="标题" align="center" />
      <el-table-column prop="createTime" label="提交日期" align="center" />
      <el-table-column label="状态" align="center">
        <template #default="scope">
          <div
            class="status-tag"
            :class="scope.row.status == 'IN_PROGRESS' ? 'blue' : 'green'"
          >
            {{ scope.row.status == "IN_PROGRESS" ? "未解决" : "已解决" }}
          </div>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" width="260">
        <template #default="scope">
          <div style="padding: 8px">
            <el-badge
              :value="scope.row.unReadNum"
              :hidden="scope.row.unReadNum == 0"
              class="item"
            >
              <div class="status-tag green cursor" @click="handleChat(scope.row)">
                留言
              </div>
            </el-badge>
            <div class="status-tag red cursor" @click="handleDelete(scope.row)">删除</div>
            <div
              class="status-tag yellow cursor"
              v-if="scope.row.status == 'IN_PROGRESS'"
              @click="handleClose(scope.row)"
            >
              关闭
            </div>
          </div>
        </template>
      </el-table-column>
    </el-table>
    <div class="page-footer">
      <el-pagination
        :total="total"
        v-model:current-page="queryParams.params.pageNum"
        v-model:page-size="queryParams.params.pageSize"
        layout="total, prev, pager, next"
        small
        @size-change="getList"
        @current-change="getList"
      />
    </div>
    <el-dialog
      v-model="chatShow"
      title="工单留言"
      center
      width="1080px"
      @close="closeDialog"
    >
      <div class="chat-content">
        <div class="chat-box">
          <div
            v-for="(item, index) in chatList"
            :key="index"
            class="chat-item"
            :class="item.userType == '00' ? 'left' : 'right'"
          >
            <el-image
              style="width: 50px; height: 50px; border-radius: 4px"
              :src="filterAvatar(item)"
            />
            <div class="chat-row">
              <div class="time">
                {{ item.createTime || "" }}
              </div>
              <div class="chat-msg">
                <template v-if="item.contentType == 2">
                  <el-image
                    style="width: 200px; height: 200px"
                    :src="baseURL + item.content"
                  />
                </template>
                <template v-else>
                  {{ item.content || "-" }}
                </template>
              </div>
            </div>
          </div>
        </div>
        <el-input
          :disabled="disabled"
          v-model="chatSend.content"
          :rows="9"
          type="textarea"
          :placeholder="disabled ? '工单已关闭' : '请输入留言内容'"
        />
        <el-button
          :disabled="disabled"
          type="primary"
          class="send-btn"
          @click="handleSend"
        >
          发送
        </el-button>
      </div>
    </el-dialog>
    <problem-modal v-model:show="addShow" @change="getList" />
  </div>
</template>
<script setup lang="ts">
import { computed, onMounted, ref } from "vue";
import { ElMessage, ElMessageBox } from "element-plus";
import {
  queryOrderList,
  deleteOrder,
  updateOrder,
  detailList,
  sendChat,
} from "@/api/control/order";
import defaultImg from "@/assets/img/common/default.png";
import useSystemStore from "@/store/main/system/system";
const systemStore = useSystemStore();

const queryParams = ref({
  params: {
    pageNum: 1,
    pageSize: 10,
  },
});
const baseURL = import.meta.env.VITE_BASE_URL;
const addShow = ref(false); // 新增工单
const dataList = ref<any>([]); // 工单列表
const total = ref(0); // 总条数
const chatShow = ref(false); // 留言弹窗
const chatList = ref(); // 留言列表
const disabled = ref(false); // 是否禁用
const chatSend = ref<any>({
  orderNum: "",
  content: "",
  contentType: 1,
});

onMounted(() => {
  getList();
});

// 头像处理
const filterAvatar = computed(() => (val) => {
  if (val.avatar.indexOf("http") > -1) {
    return val.avatar;
  } else if (val.avatar.indexOf("profile") > -1) {
    return baseURL + val.avatar;
  } else {
    let logo = '/profile/avatar/2023/11/13/blob_20231113165215A001.png'
    return val.userType == '00' ? baseURL + logo : defaultImg;
  }
});

// 获取工单数据列表
const getList = () => {
  queryOrderList(queryParams.value).then((res: any) => {
    dataList.value = res.rows;
    total.value = res.total;
  });
};

// 打开留言弹窗
const handleChat = (item) => {
  disabled.value = item.status != "IN_PROGRESS";
  chatSend.value.orderNum = item.orderNum;
  chatSend.value.contentType = 1;
  chatShow.value = true;
  getChatList();
};

// 关闭留言弹窗
const closeDialog = () => {
  chatList.value = [];
  chatSend.value = {
    orderNum: "",
    content: "",
    contentType: 1,
  };
  getList();
};

// 关闭工单(修改状态)
const handleClose = (item) => {
  ElMessageBox.confirm("关闭后该条工单将被标记为已解决，确认关闭吗?", "关闭", {
    confirmButtonText: "确定",
    cancelButtonText: "取消",
    type: "warning",
  })
    .then(() => {
      let data = {
        orderNum: item.orderNum,
        status: "CLOSED",
      };
      updateOrder(data).then((res) => {
        ElMessage.success("工单已关闭");
        getList();
      });
    })
    .catch(() => {});
};

// 删除工单
const handleDelete = (row) => {
  ElMessageBox.confirm("确认删除该条工单吗?", "删除", {
    confirmButtonText: "确定",
    cancelButtonText: "取消",
    type: "warning",
  })
    .then(() => {
      deleteOrder([row.orderNum]).then(() => {
        ElMessage.success("删除成功");
        getList();
      });
    })
    .catch(() => {});
};

// 查询留言列表
const getChatList = () => {
  let data = {
    orderNum: chatSend.value.orderNum,
    params: {
      pageNum: 1,
      pageSize: 100,
    },
  };
  detailList(data).then((res: any) => {
    chatList.value = res.rows;
    dataList.value.forEach((item: any) => {
      if (item.orderNum == chatSend.value.orderNum) {
        item.unReadNum = 0;
        systemStore.orderUnread();
      }
    });
    setTimeout(() => {
      let scrollBox = document.querySelector(".chat-box") as HTMLElement;
      scrollBox.scrollTop = scrollBox.scrollHeight;
    }, 0);
  });
};

// 发送消息
const handleSend = () => {
  if (chatSend.value.content.trim() == "") {
    ElMessage.warning("请输入内容");
    return;
  }
  sendChat(chatSend.value).then((res) => {
    ElMessage.success("发送成功");
    chatSend.value.content = "";
    getChatList();
  });
};
</script>
<style lang="less" scoped>
.banner {
  width: 100%;
  height: 193px;
  background: rgba(245, 247, 250, 0.2);
  box-shadow: -5px 5px 20px 0px #e3e8f0;
  position: relative;
  .title {
    font-size: 40px;
    font-weight: 600;
    color: #157dfa;
    position: absolute;
    top: 40px;
    right: 360px;
  }
  .add_btn {
    width: 214px;
    height: 36px;
    position: absolute;
    bottom: 40px;
    right: 425px;
  }
  .ban {
    width: 170px;
    height: 160px;
    background: url("@/assets/img/control/content/image69.png") no-repeat center center /
      100% 100%;
    position: absolute;
    top: 17px;
    right: 60px;
  }
}
.con-title {
  width: 100%;
  height: 25px;
  display: flex;
  align-items: center;
  margin: 20px 0;
  span:nth-child(1) {
    display: flex;
    align-items: center;
    img {
      width: 14px;
      height: 14px;
    }
  }
  span:nth-child(2) {
    display: flex;
    align-items: center;
    margin-left: 10px;
    font-size: 16px;
  }
}
.con {
  width: 100%;
  height: 240px;
  display: flex;
  flex-direction: column;
}
.status-tag {
  display: inline-block;
  min-width: 64px;
  height: 34px;
  line-height: 34px;
  padding: 0 10px;
  border: none;
  margin-left: 10px;
  &:first-child {
    margin-left: 0;
  }
}
.cursor {
  cursor: pointer;
}
.blue {
  background-color: #f6faff;
  color: #157dfa;
}
.green {
  background-color: #effff4;
  color: #50b674;
}
.red {
  background-color: #ffe7e7;
  color: #ff3939;
}
.yellow {
  background-color: #fff8e6;
  color: #ffa439;
}
.page-footer {
  display: flex;
  justify-content: flex-end;
  margin: 20px 0;
}

/deep/.el-textarea__inner {
  resize: none;
  outline: none;
  border-radius: 0;
}

.chat-content {
  position: relative;
  .chat-box {
    min-height: 120px;
    max-height: 400px;
    overflow-y: auto;
    padding: 20px;
    border: 1px solid #ebeef5;
    scroll-behavior: smooth;
    .chat-item {
      display: flex;
      margin-bottom: 20px;
      .el-image {
        margin-top: 30px;
      }
      .chat-row {
        max-width: calc(100% - 100px);
        display: flex;
        flex-direction: column;
      }
      .chat-msg {
        background: #f2f6fc;
        padding: 8px 20px;
        line-height: 34px;
        border-radius: 8px;
        position: relative;
        &::after,
        &::before {
          display: none;
          content: "";
          width: 0;
          height: 0;
          border-top: 6px solid transparent;
          border-bottom: 6px solid transparent;
          position: absolute;
          top: 10px;
        }
      }
      .time {
        height: 30px;
        color: #ccc;
      }
    }
    .left {
      justify-content: flex-start;
      .chat-row {
        align-items: flex-start;
      }
      .chat-msg {
        margin-left: 10px;
        &::before {
          display: block;
          border-right: 6px solid #f2f6fc;
          left: -6px;
        }
      }
      .time {
        margin-left: 10px;
        text-align: left;
      }
    }
    .right {
      flex-direction: row-reverse;
      .chat-row {
        align-items: flex-end;
      }
      .chat-msg {
        margin-right: 10px;
        &::after {
          display: block;
          border-left: 6px solid #f2f6fc;
          right: -6px;
        }
      }
      .time {
        text-align: right;
        margin-right: 10px;
      }
    }
  }
  .send-btn {
    width: 80px;
    height: 30px;
    position: absolute;
    right: 20px;
    bottom: 20px;
  }
}
</style>
<style>
.title-style {
  height: 60px;
  background-color: #f6faff !important;
}
</style>
